package com.jack.helloworld.liveWallpaper;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;

import com.jack.helloworld.R;

public class LiveWallpaperPreview extends Activity {

    public static final int LIVE_WALLPAPER_REQUEST_CODE = 0x001;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        Intent intent = new Intent();

        if (Build.VERSION.SDK_INT >= 16) {
            /*
             * Open live wallpaper preview (API Level 16 or greater).
             */
            intent.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
            intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, VideoLiveWallpaperService.class));
        } else {
            /*
             * Open live wallpaper picker (API Level 15 or lower).
             *
             * Display a quick little message (toast) with instructions.
             */
            intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
            Resources res = getResources();
            String hint = res.getString(R.string.picker_toast);
            Toast toast = Toast.makeText(this, hint, Toast.LENGTH_LONG);
            toast.show();
        }

        startActivityForResult(intent, LIVE_WALLPAPER_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == LIVE_WALLPAPER_REQUEST_CODE) {
            finish();
        }
    }

}
